package com.ruoyi.common.log.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.log.config.MinioUpload;
import com.ruoyi.common.log.config.R;
import com.ruoyi.common.log.domain.City;
import com.ruoyi.common.log.domain.Retail;
import com.ruoyi.common.log.service.CityService;
import com.ruoyi.common.log.service.RetailService;
import com.ruoyi.common.log.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("retail")
public class RetailController {
@Autowired
    private RetailService retailService;
@Autowired
private RedisTemplate  redisTemplate;
@Autowired
private CityService cityService;
//修改
    @RequestMapping("update")
    public Result updata(@RequestBody Retail retail){
        boolean update = retailService.Retailupdate(retail);
        return Result.success(update);
    }
//上传
@RequestMapping("upload")
public String upload(MultipartFile file){
    String s = MinioUpload.uploadPic(file);
    return s;
}
//查询
@RequestMapping("list")
    public Result list(@RequestBody Retail retail, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize){
    String[] createTimes = retail.getCreateTimes();
    if(createTimes!=null&&createTimes.length>0){
        retail.setStartTime(createTimes[0]);
        retail.setEndTime(createTimes[1]);
    }
    PageHelper.startPage(pageNum,pageSize);
    List<Retail>list=retailService.lists(retail);
    PageInfo<Retail> info = new PageInfo<>(list);
    return Result.success(info);

}
//添加
@RequestMapping("add")
    public Result add(@RequestBody Retail retail){
    String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();
    System.err.println(snowflakeNextIdStr);
    retail.setShopId(snowflakeNextIdStr);
    boolean save=retailService.add(retail);
    return Result.success(save);
    }
    //城市树形
    @RequestMapping("getTree")
    public Result getTree() {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<City> getlist = cityService.getlist();
        List<Tree<Object>> treeNodes = TreeUtil.<City, Object>build(getlist, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityname());
                });
        return Result.success(treeNodes);
    }
}
